// pages/bianji/bianji.js
const app = getApp()

var api = require('../../../../js/api.js');
var utilH = require('../../../../js/utilHeart.js');

Page({

    /**
     * 页面的初始数据
     */
    data: {

        logo: '',
        user: {

            userId: 0,
            userInfoId: 0,
            userWxId: 0,

            phone: '',
            code: '',
            iscode: null,
            codename: '获取验证码',

            birth: '2001-01-01',
            height: '',
            weight: '',

            wxHeadUrl: null,
            wxNickname: null,
            wxSex: "0",
        },
        usersIndex: 0,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this

        let index = options.usersIndex
        let userInfoId = options.userInfoId
        app.log(index)
        // if (index != undefined || index != null) {
            that.setData({
                user: app.globalData.users[index],
                'user.userInfoId': parseInt(userInfoId),
                usersIndex:index,
            })
        // }


        app.log(that.data.user)

        that.setData({
            'user.birth': that.data.user.birth != null ? that.data.user.birth.substr(0, 10) : '2000-01-01',
            logo: app.globalData.logo
        })


    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    bindDateChange: function (e) {
        var that = this
        app.log('picker发送选择改变，携带值为', e.detail.value)
        that.setData({
            'user.birth': e.detail.value
        })
    },
    radioChange: function (e) {
        var that = this
        app.log('radio发生change事件，携带value值为：', e.detail.value)
        that.setData({
            'user.wxSex': e.detail.value
        })
    },

    formSubmit: function (e) {
        var that = this
        app.log(that.data.user)
        app.log('form发生了submit事件，携带数据为：', e.detail.value)

        that.setData({
            'user.wxNickname': e.detail.value.wxNickname,
            'user.wxSex': e.detail.value.wxSex,
            'user.birth': e.detail.value.birth,
            'user.height': e.detail.value.height,
            'user.weight': e.detail.value.weight,
            'user.phone': e.detail.value.phone,
            'user.code': e.detail.value.code,

        })


        let msg = ''
        var RegExp = /^\d+$/;
        if (that.data.user.wxNickname == '' || that.data.user.wxNickname == null) {
            msg = '昵称不能为空'
        } else if (that.data.user.wxNickname.length > 10) {
            msg = '昵称长度超限'
        } else if (that.data.user.wxSex == '') {
            msg = '性别必选'
        } else if (that.data.user.birth == null || that.data.user.birth == '') {
            msg = '生日必选'
        } else if (that.data.user.height == '') {
            msg = '身高必填'
        } else if (that.data.user.weight == '') {
            msg = '体重必填'
        } else if (!RegExp.test(that.data.user.height)) {
            msg = '身高只能是正整数'
        } else if (!RegExp.test(that.data.user.weight)) {
            msg = '体重只能是正整数'
        } else if (!RegExp.test(that.data.user.phone) && that.data.usersIndex != 0) {
            msg = '手机号不能为空'
        } else if (!RegExp.test(that.data.user.code) && that.data.usersIndex != 0) {
            msg = '验证码不能为空'
        }


        if (msg != '') {
            wx.showToast({
                title: msg,
                icon: 'none',

                duration: 3000
            })
        } else {
            wx.showLoading({
                title: '处理中...',
            })

            setTimeout(function () {
                wx.hideLoading()
            }, 5000)

            app.log('====>>待处理数据')
            app.log(that.data.user)
            //save
            if (that.data.user.userInfoId > 0) {
                //put
                api.updInfo(that.data.user).then(res => {
                    app.log('修改成功')
                    wx.hideLoading()
                    if (res.data.code == 1001){
                        setTimeout(function () {
                            that.toGeren()
                        }, 3000)
                    }

                })
            } else {
                //add
                api.addFriend(that.data.user).then(res => {
                    app.log('新增成功')
                    wx.hideLoading()
                    if (res.data.code == 1001){
                        setTimeout(function () {
                            that.toGeren()
                        }, 3000)
                    }

                })

            }
        }


    },
    uploadImg: function () {
        var that = this
        wx.chooseImage({
            success(res) {
                const tempFilePaths = res.tempFilePaths

                api.uploadFile(tempFilePaths[0]).then(res => {
                    that.setData({
                        'user.wxHeadUrl': res
                    })
                })
            }
        })
    },
    toGeren: function () {
        var len = getCurrentPages()
        app.log('====>>完成')
        app.log(len)

        api.findAllInfos().then(res => {
            app.globalData.users = res.data.data


            wx.navigateBack({
                delta: 1
            })

        })

    },
    toShuoming: function (e) {
        let value = e.currentTarget.dataset.val
        wx.navigateTo({
            url: "/pages/index/shuoming/shuoming?val=" + value
        })
    },

    // getCode: function () {
    //     app.log(this.data.user)
    //
    //     var a = this.data.user.phone;
    //     var that = this;
    //     var myreg = /^(14[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$$/;
    //     if (this.data.user.phone == "") {
    //         wx.showToast({
    //             title: '手机号不能为空',
    //             icon: 'none',
    //             duration: 1000
    //         })
    //         return false;
    //     } else if (!myreg.test(this.data.user.phone)) {
    //         wx.showToast({
    //             title: '请输入正确的手机号',
    //             icon: 'none',
    //             duration: 1000
    //         })
    //         return false;
    //     } else {
    //         api.getCode(that.data.user.phone)
    //             .then(res => {
    //                 if (res) {
    //                     that.setData({
    //                         'user.iscode': res
    //                     })
    //                     var num = 61;
    //                     var timer = setInterval(function () {
    //                         num--;
    //                         if (num <= 0) {
    //                             clearInterval(timer);
    //                             that.setData({
    //                                 'user.codename': '重新发送',
    //                                 'user.disabled': false
    //                             })
    //
    //                         } else {
    //                             that.setData({
    //                                 'user.codename': num + "s"
    //                             })
    //                         }
    //                     }, 1000)
    //                 }
    //             })
    //
    //     }
    //
    //
    // },

    //获取验证码
    getVerificationCode() {

        var that = this;
        let phone = that.data.user.phone;
        var myreg = /^1[3-9]\d{9}$/;
        if (phone == "" || phone == null) {
            wx.showToast({
                title: '手机号不能为空',
                icon: 'none',
                duration: 1000
            })
            return false;
        } else if (!myreg.test(phone)) {
            wx.showToast({
                title: '请输入正确的手机号',
                icon: 'none',
                duration: 1000
            })
            return false;
        } else {
            api.getCode(phone).then(res => {
                if (res) {
                    that.setData({
                        'user.disabled': true,
                        'user.iscode': res
                    })
                    var num = 61;
                    var timer = setInterval(function () {
                        num--;
                        if (num <= 0) {
                            clearInterval(timer);
                            that.setData({
                                'user.codename': '重新发送',
                                'user.disabled': false
                            })

                        } else {
                            that.setData({
                                'user.codename': num + "s"
                            })
                        }
                    }, 1000)
                }
            })

        }


    },


    getPhoneValue: function (e) {
        this.setData({
            'user.phone': e.detail.value
        })
    },
    getCodeValue: function (e) {
        app.log('==>>验证码')
        app.log(e.detail.value)
        this.setData({
            'user.code': e.detail.value
        })
    }

})
